package com.niodata.dp.task.service.impl;

import com.niodata.dp.core.OwnerType;
import com.niodata.dp.task.dao.PluginExecTemplateMapper;
import com.niodata.dp.task.dao.PluginPackageMapper2;
import com.niodata.dp.task.entity.JobInfo;
import com.niodata.dp.task.entity.JobNodeStyleInfo;
import com.niodata.dp.task.entity.JobParams;
import com.niodata.dp.task.entity.PluginExecTemplate;
import com.niodata.dp.task.entity.PluginPackage;
import com.niodata.dp.task.entity.TaskInfo;
import com.niodata.dp.task.service.PluginExecTemplateManager;
import com.niodata.dp.util.IdUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component("azBasedPluginExecTemplateManager")
public class AzBasedPluginExecTemplateManager implements PluginExecTemplateManager {

  @Autowired
  @Qualifier("azTaskManager")
  private AzBasedTaskManagerV2 taskManager;

  @Autowired
  private PluginExecTemplateMapper templateMapper;

  @Autowired
  private PluginPackageMapper2 packageMapper;

  @Override
  public PluginExecTemplate getTemplate(String name, String version, long userId,
      long groupId) {
    return this.templateMapper.getByOwnerId(groupId, OwnerType.GROUP.name(), name, version);
  }

  @Override
  public List<PluginExecTemplate> getTemplateByPluginName(String name, long userId, long groupId) {
    return this.templateMapper.getByOwnerIdAndName(groupId, OwnerType.GROUP.name(), name);
  }

  @Override
  public PluginExecTemplate createTemplate(String name, String version, long userId, long groupId)
      throws Exception {
    PluginExecTemplate template = new PluginExecTemplate();
    String id = IdUtil.uuid();
    PluginPackage pkg = packageMapper.getPluginByNameVersion(name, version);
    if (pkg == null) {
      List<PluginPackage> pkgs = packageMapper.getPluginByName(name);
      if (pkgs != null && pkgs.size() > 0
          && pkgs.get(pkgs.size() - 1).getPkgVersion().compareTo(version) > 0) {
        pkg = pkgs.get(pkgs.size() - 1);
      }
    }

    String taskName = "Task" + groupId + pkg.getPkgName() + pkg.getPkgVersion().split("-")[0];
    template.setTaskName(taskName);
    template.setId(id);
    template.setCreateTime(new Date());
    template.setOwnerId(groupId);
    template.setOwnerType(OwnerType.GROUP.name());
    template.setPluginName(pkg.getPkgName());
    template.setPluginVersion(pkg.getPkgVersion());
    TaskInfo taskInfo = new TaskInfo();
    taskInfo.setTaskId(IdUtil.uuid());
    taskInfo.setOwnerType(OwnerType.GROUP.name());
    taskInfo.setOwnerId(groupId);
    taskInfo.setCreateTime(new Date());
    taskInfo.setName(template.getTaskName());
    JobInfo jobInfo = new JobInfo();

    List<JobInfo> jobList = new ArrayList<>(1);
    jobList.add(jobInfo);
    jobInfo.setJobType(pkg.getJobType());
    JobParams jobParams = new JobParams();
    jobParams.setParams(new HashMap<>());
    jobInfo.setParams(jobParams);
    jobInfo.setPkgType(pkg.getPkgType());
    jobInfo.setPkgLang(pkg.getPkgLang());
    jobInfo.setPkgPath(pkg.getPkgPath());
    jobInfo.setPkgName(pkg.getPkgName());
    jobInfo.setNodeStyleInfo(getNodeStyle());
    jobInfo.setPkgId(pkg.getPkgId());
    jobInfo.setPkgVersion(pkg.getPkgVersion());
    jobInfo.setJobId(IdUtil.uuid());
    jobInfo.setJobName(name.toLowerCase());
    jobInfo.setTaskId(taskInfo.getTaskId());
    taskInfo.setJobInfoList(jobList);
    taskInfo.setTemplate(true);
    taskInfo.setDescription("task " + taskName);
    taskInfo.setScheduleCron("30 17,5 * * ?");
    taskManager.createTask(taskInfo);
    taskManager.submitTask(taskInfo.getTaskId());
    this.templateMapper.save(template);
    return template;
  }

  @Override
  public void deleteTemplate(String name, String version, long userId, long groupId) {
    PluginExecTemplate template =
        this.templateMapper.getByOwnerId(groupId, OwnerType.GROUP.name(), name, version);
    if (template != null) {
      templateMapper.delete(template.getId());
      taskManager.deleteTaskByName(template.getTaskName());
    }
  }

  private JobNodeStyleInfo getNodeStyle() {
    JobNodeStyleInfo styleInfo = new JobNodeStyleInfo();
    styleInfo.setClassName("_box_flow");
    Map map = new HashMap<>();
    map.put("left", "22%");
    map.put("top", "15%");
    map.put("lineHeight", "40px");
    styleInfo.setStyleInfo(map);
    return styleInfo;
  }
}
